//
//  BWWelcomeViewController.swift
//  weibo
//
//  Created by libowen on 16/5/23.
//  Copyright © 2016年 libowen. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class BWWelcomeViewController: UIViewController {
    override func loadView() {
        view = backView
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        setUI()
    }
    private func setUI() {
        view.addSubview(iconView)
        view.addSubview(nameLable)
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.view.snp_centerX)
            make.bottom.equalTo(self.view.snp_bottom).offset(-160)
            make.width.equalTo(90)
            make.height.equalTo(90)
        }
        nameLable.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_centerX)
            make.top.equalTo(iconView.snp_bottom).offset(16)
        }
        
        iconView.sd_setImageWithURL(BWUserAccountModel.sharedAccountViewModel.headURL)
        
        iconView.cornerRadius = 45
        iconView.clipsToBounds = true
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        startAnimation()
    }
    private func startAnimation() {
        let offset = -ScreenHeight + 160
        UIView.animateWithDuration(1.2, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 6, options: [], animations: { () -> Void in
            self.iconView.snp_updateConstraints(closure: { (make) -> Void in
                make.bottom.equalTo(offset)
            })
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                UIView.animateWithDuration(0.5, animations: { () -> Void in
                    self.nameLable.alpha = 1
                    }, completion: { (_) -> Void in
                        print("👌")
                    NSNotificationCenter.defaultCenter().postNotificationName(SwitchRootViewController, object: "main")
                })
        }
    }
    override func prefersStatusBarHidden() -> Bool {
        return true
    }
    private lazy var backView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    private lazy var nameLable: UILabel = {
        let l = UILabel()
        
        l.text = BWUserAccountModel.sharedAccountViewModel.userAccount?.name
        l.font = UIFont.systemFontOfSize(18)
        l.textColor = UIColor.darkGrayColor()
        l.alpha = 0
        l.sizeToFit()
        return l
    }()


}






















